package covertjava.profile;

import javax.swing.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * <p>Implements Factory pattern for points creation</p>
 * <p>Copyright: Copyright (c) 2004 Sams Publishing</p>
 * @author Alex Kalinovsky
 * @version 1.0
 */
public class PointFactory {

    protected ArrayList points = new ArrayList();
    protected static PointFactory instance = new PointFactory();

    public Point createPoint(int x, int y) {
        Point point = new Point(x, y);
        this.points.add(point);
        return point;
    }

    public void removePoint(Point point) {
        this.points.remove(point);
    }


    public void printTestPoints() {
        for (int i = 0; i < 5; i++) {
            Point point = createPoint(i, i);
            System.out.println("Point = " + point);
        }
    }


    public static PointFactory getInstance() {
        return instance;
    }


    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame("Points Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JButton button = new JButton("Print Test Points");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                PointFactory.getInstance().printTestPoints();
            }
        });
        frame.getContentPane().add(button);
        frame.setSize(200, 100);
        frame.setVisible(true);
    }


}
